package study.itwill.jsp;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

public class CookieBox {
	
	// Logger 클래스의 인스턴스를 받아온다.
	static Logger logger = Logger.getLogger(CookieBox.class);
	
	private Map<String, Cookie> cookieMap = new java.util.HashMap<String, Cookie>();
	private HttpServletResponse response = null;
	
	public CookieBox(HttpServletRequest request,
			HttpServletResponse response) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i=0; i<cookies.length; i++) {
				cookieMap.put(cookies[i].getName(), cookies[i]);
			}
		}
		
		this.response = response;
	}
	
	public boolean addCookie(String name, String value, 
			String domain, String path, int maxAge) {
		boolean result = false;
		
		try {
			Cookie c = CookieBox.createCookie(name, value, 
					domain, path, maxAge);
			this.response.addCookie(c);
			result = true;
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			logger.error(e.getStackTrace());
		}
		
		return result;
	}
	
	
	
	
	public boolean addCookie(String name, String value, 
			String path, int maxAge) {
		boolean result = false;
		
		try {
			Cookie c = CookieBox.createCookie(name, value, 
					path, maxAge);
			this.response.addCookie(c);
			result = true;
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			logger.error(e.getStackTrace());
		}
		
		return result;
	}
	
	public boolean addCookie(String name, String value, 
			int maxAge) {
		boolean result = false;
		
		try {
			Cookie c = CookieBox.createCookie(name, value, maxAge);
			this.response.addCookie(c);
			result = true;
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			logger.error(e.getStackTrace());
		}
		
		return result;
	}
	
	public boolean addCookie(String name, String value) {
		boolean result = false;
		
		try {
			Cookie c = CookieBox.createCookie(name, value);
			this.response.addCookie(c);
			result = true;
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			logger.error(e.getStackTrace());
		}
		
		return result;
	}
	
	public static Cookie createCookie(String name, String value) throws IOException {
		return new Cookie(name, URLEncoder.encode(value, "utf-8"));
	}
	
	public static Cookie createCookie(String name, String value, int maxAge) throws IOException {
		Cookie cookie = new Cookie(name, URLEncoder.encode(value, "utf-8"));
		cookie.setMaxAge(maxAge);
		return cookie;
	}
	
	public static Cookie createCookie(String name, String value, String path, int maxAge) throws IOException {
		Cookie cookie = new Cookie(name, URLEncoder.encode(value, "utf-8"));
		cookie.setPath(path);
		cookie.setMaxAge(maxAge);
		return cookie;
	}
	
	public static Cookie createCookie(String name, String value, String domain, String path, int maxAge) throws IOException {
		Cookie cookie = new Cookie(name, URLEncoder.encode(value, "utf-8"));
		cookie.setDomain(domain);
		cookie.setPath(path);
		cookie.setMaxAge(maxAge);
		return cookie;
	}
	
	public Cookie getCookie(String name) {
		return cookieMap.get(name);
	}
	
	public String getValue(String name) throws IOException {
		Cookie cookie = cookieMap.get(name);
		if (cookie == null) {
			return null;
		}
		return URLDecoder.decode(cookie.getValue(), "utf-8");
	}
	
	public boolean exists(String name) {
		return cookieMap.get(name) != null;
	}
}
